package com.systemsjr.mpf.account;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.systemsjr.mpf.IntentHandler;
import com.systemsjr.mpf.R;
import com.systemsjr.mpf.expense.ManageExpenseActivity;

public class AccountInstanceList extends ListActivity {

	private SimpleCursorAdapter adapter;
	private ListView listView;
	private Cursor c;
	public static final String[] PROJECTIONS = { AccountInstance.ID,
			AccountInstance.INSTANCE, AccountInstance.ACCOUNT_FK,
			AccountInstance.START_DATE, AccountInstance.CLOSE_DATE,
			AccountInstance.BUDGET, AccountInstance.SPENT };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_account_instance_list);

		String[] from = { AccountInstance.INSTANCE, AccountInstance.BUDGET,
				AccountInstance.SPENT };
		AccountInstance accountInstance = new AccountInstance();
		c = getContentResolver().query(AccountInstance.CONTENT_URI, from, " CLOSE_DATE IS NULL",
				null, AccountInstance.START_DATE + " DESC");

		Bundle bundle = getIntent().getExtras();
		if (bundle != null && bundle.size() > 0) {
			if (bundle.getString(AccountInstance.INSTANCE) != null) {
				accountInstance.setInstance(bundle
						.getString(AccountInstance.INSTANCE));
			}

			if (bundle.getDouble(AccountInstance.BUDGET) > 0) {
				accountInstance.setBudget(bundle
						.getDouble(AccountInstance.BUDGET));
			}

			if (bundle.getDouble(AccountInstance.BUDGET) > 0) {
				accountInstance.setSpent(bundle
						.getDouble(AccountInstance.BUDGET));
			}

			if (bundle.get(AccountInstance.ACCOUNT_FK) != null) {
				accountInstance.setAccount((Account) bundle
						.get(AccountInstance.ACCOUNT_FK));
			}
		}

		String selection = AccountInstance.INSTANCE
				+ " LIKE "
				+ (accountInstance.getInstance() == null
						|| accountInstance.getInstance().length() > 0 ? "'%'"
						: "'%" + accountInstance.getInstance() + "%'");

		selection = selection
				+ " AND "
				+ AccountInstance.ACCOUNT_FK
				+ (accountInstance.getAccount() == null ? " > 0" : " = "
						+ accountInstance.getAccount().getId());

		c = getContentResolver().query(AccountInstance.CONTENT_URI,
				PROJECTIONS, selection, null,
				AccountInstance.START_DATE + " ASC");
		startManagingCursor(c);
		int[] to = { R.id.list_instance, R.id.list_budget,
				R.id.list_used_amount };

		System.out.println(c.getCount());

		adapter = new SimpleCursorAdapter(this,
				R.layout.account_instance_list_entry, c, from, to, 0);
		listView = getListView();

		listView.setAdapter(adapter);

		Button instanceButton = (Button) findViewById(R.id.addInstanceButton);
		instanceButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(AccountInstanceList.this,
						ManageAccountInstanceActivity.class);
				startActivity(intent);
			}
		});

		Button expenseButton = (Button) findViewById(R.id.addExpenseButton);
		expenseButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(AccountInstanceList.this,
						ManageExpenseActivity.class);
				startActivity(intent);

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.account_instance_list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		Intent intent = IntentHandler.handleOnOptionsItemSelected(this,
				item.getItemId());
		startActivityForResult(intent, 1);
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onResume() {
		super.onResume();
		// c.requery();
		adapter.notifyDataSetChanged();
		listView.invalidateViews();
	}
}
